let app = getApp();
Component({

  lifetimes: {
    attached() {
      
     
        // 获取时间
        function  getDayDate(n){
          var time=(new Date).getTime()+24*60*60*1000*n;
          var yesterday=new Date(time);
          var month=yesterday.getMonth();
          var day=yesterday.getDate();
          yesterday= (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0"
          + (yesterday.getMonth() + 1)) + "-" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0"
          + (yesterday.getDate()));
       
        return yesterday
      }
  // 处理日期
  let today=getDayDate(0)
  let ttoday=getDayDate(1)
  let tttoday=getDayDate(2)
  let ttttoday=getDayDate(3)
  this.setData({
    navlist:[
      {data:today,name:"今天",select:true},
      {data:ttoday,name:"明天",select:false},
      {data:tttoday,name:"后天",select:false},
      {data:ttttoday,name:"外后天",select:false},
    ],
    currentSelectDay:today
  })

      this.init()
      // 在组件实例进入页面节点树时执行
       



    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的一些选项
   */
  options: {
   
  },
  /**
   * 组件的对外属性
   */
  properties: {
    date: {
      type: String,
      default: ''
    }
   
  },
  /**
   * 组件的初始数据
   */
  data: {
     navlist:[],
     dateArr:[],
     currentSelectDay:"",
     currentSelectYear:"",
     time:"",
     minute:0,
     hour:0,
     showText:"",

     moreDate:"",
     isHide:true
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 给外层提供的 调用方法
    passTime(hour,minute){
      console.log("kkkkk")
      function  getDayDate(n){
        var time=(new Date).getTime()+24*60*60*1000*n;
        var yesterday=new Date(time);
        var month=yesterday.getMonth();
        var day=yesterday.getDate();
        yesterday= (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0"
        + (yesterday.getMonth() + 1)) + "-" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0"
        + (yesterday.getDate()));
     
      return yesterday
    }
    
     //更新数据
     this.triggerEvent('getShowText', "")

      // 刷新nav
      let temp =JSON.parse(JSON.stringify(this.data.navlist))
      temp.forEach((item,index)=>{
          if(index == 0){
            item.select=true
          }else{
            item.select=false
          }
      })

      this.setData({
        hour:hour,
        minute:minute,
        isHide:true,
        navlist:temp,
        currentSelectDay:getDayDate(0),
        currentSelectYear:new Date().getFullYear()
      
      })
      this.init()
    },
    DateChange(e){
      // e.detail.value
      // this.init()
      // 选好日期 将 nav 隐藏
    


    
      // 2021-01-22
      let pickerSelect= e.detail.value.split("-")[1]+"-"+e.detail.value.split("-")[2]
      this.setData({
        currentSelectDay:pickerSelect,
        currentSelectYear:e.detail.value.split("-")[0],
        isHide:false
      })
      this.init()
     
    
    


    },
    init(){
         // 获取时间
         function  getDayDate(n){
          var time=(new Date).getTime()+24*60*60*1000*n;
          var yesterday=new Date(time);
          var month=yesterday.getMonth();
          var day=yesterday.getDate();
          yesterday= (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0"
          + (yesterday.getMonth() + 1)) + "-" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0"
          + (yesterday.getDate()));
       
        return yesterday
      }
  // 处理日期
  let today=getDayDate(0)
  let ttoday=getDayDate(1)
  let tttoday=getDayDate(2)
  let ttttoday=getDayDate(3)
 


  // 处理 时间
  let hourArr=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","00"]
  let minuteArr=["00","15","30","45"]
  

 let currentHour=new Date().getHours()< 10? "0" + new Date().getHours() : new Date().getHours()
 let currentMinute=new Date().getMinutes()< 10? "0" + new Date().getMinutes() : new Date().getMinutes()
//  console.log(currentHour,currentMinute)
  /**
   * 1.排列组合
   */
  let newArr=[]
  for(let i= 0;i< hourArr.length;i++){           
     for(let n=0;n<minuteArr.length;n++){               
       let str=hourArr[i]+":" +minuteArr[n]
         newArr.push(str)
     }
  }
  //将数组 后三位移位到前面
  let tempArr=[newArr[newArr.length-3],newArr[newArr.length-2],newArr[newArr.length-1]]
  newArr.pop()
  newArr.pop()
  newArr.pop()
  newArr=tempArr.concat(newArr)

  /**
   * 2.过滤过时的时间
   */
  // console.log("todayLLLL",today)
  // console.log("todayLLLLKKKKKK",this.data.currentSelectDay)

  if(today+"" == this.data.currentSelectDay){
    // console.log(currentHour+":"+currentMinute+"")
    // console.log()
  
    let filterHourArr=[]
    newArr.forEach(item=>{
      // 对 00:00 单独处理
       if(item == "00:00"){
         filterHourArr.push(item)
       }
      //  小时数 相等 比较 分钟
       if(Number(item.split(":")[0]) == Number(currentHour)  ){
          if( Number(item.split(":")[1]) >  Number(currentMinute) ){
            filterHourArr.push(item)
          }
       }
       //比较小时数
       if(Number(item.split(":")[0]) > Number(currentHour)){
        filterHourArr.push(item)
       }
    })
    
    newArr=filterHourArr
  }

 
  // 转成JSON
  let newJson=[]
  newArr.forEach((nItem,index)=>{
     let obj={}
     obj.index=index
     obj.name=nItem
     obj.select=false

     newJson.push(obj)
  })


  // 分组
  let length=newArr.length/5
  let finalArr=[]
  for(let m=1;m<length+1;m++){
    //  let list=[]
     let listObj={}
    // console.log(newJson.slice((m-1)*5,(m-1)*5+5))
    // list.push(newJson.slice((m-1)*5,(m-1)*5+5))
    listObj.index=m
    listObj.list=newJson.slice((m-1)*5,(m-1)*5+5)

    finalArr.push(listObj)
  }
  // console.log(length)
  // console.log(finalArr)
  this.setData({
    dateArr:finalArr,
    moreDate:new Date().getFullYear()+"-"+ today
  })
    },
    changeNav(e){
      console.log(e)
      let temp =JSON.parse(JSON.stringify(this.data.navlist))
      temp.forEach(item=>{
        item.select=false
        if(item.data == e.currentTarget.dataset.item.data){
          item.select=true
        }
      })
      // console.log(e.currentTarget.dataset.item.data)
      this.setData({
        navlist:temp,
        currentSelectDay:e.currentTarget.dataset.item.data+""
      })
      this.init()
      this.triggerEvent('getShowText', "")
    },
    selectTime(e){
      if(e.currentTarget.dataset.item.name !== "00:00"){
        //  console.log(e.currentTarget.dataset.item.name)
       let tempList=JSON.parse(JSON.stringify(this.data.dateArr))
       
       let newArr=[]
       let currentSelectTime
       for(let i=0;i<=tempList.length-1;i++){
               
           for(let k=0;k<=tempList[i].list.length-1;k++){
              tempList[i].list[k].select=false
              if(e.currentTarget.dataset.item.name+"" == tempList[i].list[k].name){
                tempList[i].list[k].select=true
                // console.log( tempList[i].list[k].name)
                currentSelectTime=tempList[i].list[k].name

                
                let hIndex=0
                let mindex=0
                let hour=0
                let minute=0
               if(  (Number(currentSelectTime.split(":")[0]) + Number(this.data.hour) ) >=24  ){
                if(hour+"" == "24"){
                  hour="00"
                  hIndex=1
                }
                     hIndex = 1
                     hour = (Number(currentSelectTime.split(":")[0]) + Number(this.data.hour) -24 ) < 10 ? "0" + (Number(currentSelectTime.split(":")[0]) + Number(this.data.hour) -24 ) : (Number(currentSelectTime.split(":")[0]) + Number(this.data.hour) -24 )  
               }else{
                if(hour+"" == "24"){
                  hour="00"
                  hIndex=1
                }
                    // console.log("abs",Math.abs( Number(currentSelectTime.split(":")[0]) + Number(this.data.hour) -24 )  )
                hIndex = 0
                hour =  Math.abs( Number(currentSelectTime.split(":")[0]) + Number(this.data.hour)    ) < 10 ? "0" + Math.abs( Number(currentSelectTime.split(":")[0]) + Number(this.data.hour)    ):Math.abs( Number(currentSelectTime.split(":")[0]) + Number(this.data.hour)    )

               }
      
               if(  (Number(currentSelectTime.split(":")[1]) + Number(this.data.minute) ) >=60 ){
                     mindex = 1
                     minute = ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute) -60 ) < 10 ? "0"+ ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute) -60 ) :  ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute) -60 )
                     if(hour+"" == "24"){
                       hour="00"
                       hIndex=1
                     }
                     hour= ( Number(hour) +1 ) < 10 ? "0" + ( Number(hour) +1 ) : ( Number(hour) +1 )
                    
               }else{
                    mindex =0
                    if(hour+"" == "24"){
                      hour="00"
                      hIndex=1
                    }
                    minute =  ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute)  ) < 10 ? "0" +  ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute)  ): ( Number(currentSelectTime.split(":")[1]) + Number(this.data.minute)  )
               }
               if(hour+"" == "24"){
                hIndex=1
                hour="00"
              }
              let ss=   hIndex == "1" ?"至次日":"至"

                


              let showText= ss+  hour+":"+minute
              // console.log(showText )
                

              this.setData({
                showText:showText
              })

              this.triggerEvent('getShowText', (this.data.currentSelectYear||new Date().getFullYear())+"-"+this.data.currentSelectDay+"  "+ tempList[i].list[k].name+ "至"   +  hour+":"+minute)

              

              }
                  console.log( new Date().getFullYear()+"-"+this.data.currentSelectDay)
              newArr.push(tempList[i].list[k])
           }
       }

     

      //  console.log(newArr)
       // 分组
        let length=newArr.length/5
        let finalArr=[]
        for(let m=1;m<length+1;m++){
          //  let list=[]
          let listObj={}
          // console.log(newJson.slice((m-1)*5,(m-1)*5+5))
          // list.push(newJson.slice((m-1)*5,(m-1)*5+5))
          listObj.index=m
          listObj.list=newArr.slice((m-1)*5,(m-1)*5+5)

          finalArr.push(listObj)
        }
        // console.log(length)
        // console.log("year", finalArr)
        this.setData({
          dateArr:finalArr
        })

      

      }


    }
  }
})